package com.rock.code.bigdata.alarmanalysis.controller;

import com.github.pagehelper.PageInfo;
import com.rock.code.base.controller.BaseController;

import com.rock.code.bigdata.alarmanalysis.service.AlarmAnalysisService;
import com.rock.code.bigdata.kpimapping.domain.TbCdeKpiRelationSrc;
import com.rock.code.common.exceptions.SystemException;
import com.rock.code.common.pager.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by SJ on 2018/10/24.
 */

@Controller
@RequestMapping("/alarmAnalysis")
public class AlarmAnalysisController extends BaseController {

    @Resource
    private AlarmAnalysisService alarmAnalysisServiceImpl;

    @RequestMapping("")
    public String dispatch() throws Exception {
        return "bigdata/alarmanalysis/alarmTopView";
    }

    @ResponseBody
    @RequestMapping("/queryResourceTop")
    public List queryResourceTop(String DATE_TYPE) {
        return alarmAnalysisServiceImpl.queryResourceAlarm(DATE_TYPE);
    }

    @ResponseBody
    @RequestMapping("/queryDetails")
    public HashMap queryDetails(String EXT_UNIT_ID, String eventClass, String DATE_TYPE, String ROWNUM, String ROW_ID) {
        List<Map> list = alarmAnalysisServiceImpl.queryDetails(EXT_UNIT_ID, eventClass, DATE_TYPE, ROWNUM, ROW_ID);
        LinkedHashMap<String, List> resultMap = new LinkedHashMap<>();
        HashMap doubleResultMap = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            //根据key获取时间：2018-12-04 10:21:40.0
            String GENERANT_TIME = map.get("GENERANT_TIME").toString();
            //以空格拆分时间，取第0位：2018-12-04
            String key = GENERANT_TIME.split("\\s+")[0];
            //若new的Map中不存在某个key，则将该key对应的value存入list中并将此list存放进new的 Map中，key不变
            if (!resultMap.containsKey(key)) {
                List newList = new ArrayList<>();
                newList.add(map);
                resultMap.put(key, newList);
            } else {//若存在某个key则直接将其value值add进该key对应的list中
                List resultList = resultMap.get(key);
                resultList.add(map);
                resultMap.put(key, resultList);
            }
        }
        doubleResultMap.put("list", list);
        doubleResultMap.put("resultMap", resultMap);
        return doubleResultMap;
    }

    @ResponseBody
    @RequestMapping("/queryAlarmClassByResource")
    public List queryAlarmClassByResource(String EXT_UNIT_ID, String DATE_TYPE) {
        return alarmAnalysisServiceImpl.queryAlarmClassByResource(EXT_UNIT_ID, DATE_TYPE);
    }

}